package com.library.service;

import java.util.ArrayList;
import java.util.List;

import com.library.common.SPageBean;
import com.library.common.SysUtil;
import com.library.dao.ICommonDAO;
import com.library.model.Bookinfo;

public class AdminBookService {
	private ICommonDAO commonDao;

	public ICommonDAO getCommonDao() {
		return commonDao;
	}

	public void setCommonDao(ICommonDAO commonDao) {
		this.commonDao = commonDao;
	}
	
	public List getAllBooksByPage(SPageBean spb){
		/*List<Object[]> list = new ArrayList<Object[]>();
		Object[] data;
		String hql = "from Bookinfo b";
		//commonDao.listByPage(hql, spb);
		List<Bookinfo> book = commonDao.list(hql);*/
		//for(Bookinfo b:book){
		  /*if(b.getStatus()==1){
				data = new Object[]{b.getBookid(),b.getBookname(),b.getBookkind(),b.getStatus()};
				list.add(data);
			}*/
			//if(b.getStatus()==-1){
				String hql1 = "from Bookinfo b inner join fetch b.userinfo";
				commonDao.listByPage(hql1, spb);
				//Object[] args = new Object[]{b.getBookid()};
				//List<Bookinfo> book1 = commonDao.list(hql1,args);
				//Bookinfo b1 = book1.get(0);
				//System.out.println(b1.getUserinfo().getUsername());
				//for(Bookinfo b1:book1){
	 			//	data = new Object[]{b1.getBookid(),b1.getBookname(),b1.getBookkind(),b1.getStatus(),
				//			b1.getStarttime(),b1.getUserinfo().getUsername()};
				//	list.add(data);
				//}
			//}
		//}
		return (List)spb.getDatas();
	}
	
	public List getStockBooksByPage(SPageBean spb){
		String hql = "from Bookinfo b where b.status=1";
		commonDao.listByPage(hql, spb);
		return (List)spb.getDatas();
	}
	
	public List checkBook(SPageBean spb){
		List<Object[]> list = new ArrayList<Object[]>();
		Object[] data;
		String hql = "from Bookinfo b";
		List<Bookinfo> book = commonDao.list(hql);
		for(Bookinfo b:book){
		  if(b.getStatus()==1){
				data = new Object[]{b.getBookid(),b.getBookname(),b.getBookkind(),b.getStatus()};
				list.add(data);
			}
		  if(b.getStatus()==-1){
				String hql1 = "from Bookinfo b inner join fetch b.userinfo where b.bookid = ?";
				Object[] args = new Object[]{b.getBookid()};
				List<Bookinfo> book1 = commonDao.list(hql1,args);
				Bookinfo b1 = book1.get(0);
				System.out.println(b1.getUserinfo().getUsername());
	 			data = new Object[]{b1.getBookid(),b1.getBookname(),b1.getBookkind(),b1.getStatus(),
							b1.getStarttime(),b1.getUserinfo().getUsername()};
				list.add(data);
			}
		}
		return list;
	}
	public List queryBook(SPageBean spb,Bookinfo book){
		String hql1 = "from Bookinfo where bookname like '%"+book.getBookname()+"%'";
		/*String hql = "from Bookinfo b where 1 = 1";
		if(SysUtil.isBlankString(book.getBookname())){
			hql += " and b.bookname like '%"+book.getBookname()+"%'";
		}
		if(book.getBookkind()!=0){
			hql += " and b.bookkind="+book.getBookkind();
		}*/
		commonDao.listByPage(hql1, spb);
		return (List)spb.getDatas();
	}
	
	public boolean addBook(Bookinfo b){
		boolean flag = false;
		try{
			commonDao.add(b);
			flag = true;
		}
		catch(Exception e){
			flag = false;
		}
		return flag;
	}
	
	public Bookinfo getBookById(int id){
		Bookinfo book = (Bookinfo)commonDao.get(Bookinfo.class, id);
		return book;
	}
	
	public boolean updateBook(Bookinfo b){
		boolean flag = false;
		try{
			commonDao.update(b);
			flag = true;
		}
		catch (Exception e) {
			flag = false;
		}
		return flag;	
	}
	
	public boolean deleteBook(int bookid){
		boolean flag = false;
		try{
			commonDao.delete(Bookinfo.class,bookid);
			flag = true;
		}
		catch (Exception e) {
			flag = false;
		}
		return flag;
	}

}
